
package com.jf.cloud.coupon.task;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.jf.cloud.common.cache.constant.CacheNames;
import com.jf.cloud.common.cache.constant.LockCacheNames;
import com.jf.cloud.common.cache.util.CacheManagerUtil;
import com.jf.cloud.coupon.manager.CouponStockManager;
import com.jf.cloud.coupon.mapper.CouponMapper;
import com.jf.cloud.coupon.service.CouponService;
import com.jf.cloud.coupon.service.CouponUserService;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.apache.commons.collections.CollectionUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author zz
 */
@Component
public class CouponTask {

    public static final Logger log = LoggerFactory.getLogger(CouponTask.class);

    @Autowired
    private CouponUserService couponUserService;
    @Autowired
    private CouponService couponService;
    @Autowired
    private CouponMapper couponMapper;
    @Autowired
    private CacheManagerUtil cacheManagerUtil;
    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private CouponStockManager couponStockManager;

    /**
     * 删除用户失效180天以上的优惠券
     */
    @XxlJob("deleteCouponUser")
    public void deleteCouponUser() {
        log.info("删除用户失效180天以上的优惠券");
        // 删除用户失效半年以上的优惠券 参考京东
        couponUserService.deleteUnValidTimeCoupons(DateUtil.offsetDay(new Date(), -180));
    }

    /**
     * 改变用户优惠券的状态(设为失效状态)
     */
    @XxlJob("changeCouponUser")
    public void changeCouponUser() {
        log.info("设置用户的过期优惠券为失效状态");
        // 设置用户的过期优惠券为失效状态
        couponUserService.updateStatusByTime();
    }

    /**
     * 改变店铺优惠券的状态(设为过期状态)
     */
    @XxlJob("changeCoupon")
    public void changeCoupon() {
        log.info("设置店铺的过期优惠券为过期状态");
        // 查询需要更新的店铺id列表
        List<Long> shopIds = couponMapper.listOverdueStatusShopIds();
        // 取消投放
        couponService.cancelPut();
        //清除缓存
        if (CollectionUtils.isNotEmpty(shopIds)) {
            for (Long shopId : shopIds) {
                cacheManagerUtil.evictCache(CacheNames.COUPON_LIST_BY_SHOP_KEY, String.valueOf(shopId));
            }
        }
    }

    /**
     * 投放优惠券
     */
    @XxlJob("putOnCoupon")
    public void putOnCoupon() {
        log.info("投放优惠券");
        // 投放优惠券
        couponService.putonCoupon();
    }

    /**
     * 正在进行活动的优惠券库存落库
     * 10s一次
     */
    @XxlJob("hotCouponStockPlacement")
    public void hotCouponStockPlacement() {
        log.info("正在进行活动的优惠券库存落库");
        List<Long> couponIds = couponMapper.listMaybeNeedStockPlacementActivity();
        if (CollUtil.isEmpty(couponIds)) {
            return;
        }
        for (Long couponId : couponIds) {
            RLock rLock = redissonClient.getLock(LockCacheNames.LOCK_COUPON_ZONE_PERFIX + couponId);
            rLock.lock();
            try {
                couponStockManager.flushStockToDb(couponId,null);
            } finally {
                rLock.unlock();
            }
        }
    }
}
